import subprocess
import time


class ActionExecutor:
    def __init__(self, device_id='emulator-5554'):
        self.device_id = device_id
        self.click_delay = 0.3  # 点击间隔防止过快操作

    def execute_actions(self, actions):
        """执行动作序列"""
        for action in actions:
            if action['type'] == 'select_plant':
                self._tap_screen(action['position'])
            elif action['type'] == 'swipe':
                self._swipe_screen(action['start'], action['end'])
            time.sleep(self.click_delay)

    def _tap_screen(self, position):
        """模拟屏幕点击"""
        cmd = f'adb -s {self.device_id} shell input tap {position[0]} {position[1]}'
        subprocess.run(cmd, shell=True)

    def _swipe_screen(self, start, end, duration=300):
        """模拟滑动操作"""
        cmd = (
            f'adb -s {self.device_id} shell input swipe '
            f'{start[0]} {start[1]} {end[0]} {end[1]} {duration}'
        )
        subprocess.run(cmd, shell=True)